import { Component, OnInit, Input, NgZone } from '@angular/core';
import { InAppBrowser } from '@ionic-native/in-app-browser/ngx';
import { Router } from "@angular/router";
import { HttpService } from 'src/app/shared/service/http.service';
@Component({
  selector: 'app-protocol-template',
  templateUrl: './protocol-template.component.html',
  styleUrls: ['./protocol-template.component.scss'],
})
//tslint:disable
export class ProtocolTemplateComponent implements OnInit {
  // @ViewChild('outHtml') htmlElement: ElementRef;
  @Input() tagData: any;
  isShowApply = false;
  inviteCode: any;
  clientType: any;
  constructor(
    private ngZone: NgZone, 
    private router: Router, 
    private iab: InAppBrowser,
    private httpService: HttpService) {
  }
  ngOnInit() {

    setTimeout(() => {
      const iframe = document.getElementById("iframe-protocol");
      try {
        let src = "";
        if (this.tagData.content) {
          src = this.tagData.content;
          iframe.setAttribute("src", src);
        } else if (this.tagData.data && this.tagData.data.content) {
          if(this.tagData.data&&this.tagData.data.quote_price&&this.tagData.data.quote_way){
            this.httpService.post('element/ljserviceContract/url', {
              'opportunity_id':this.tagData.data.opportunity_id,
              'quote_service_days':this.tagData.data.quote_service_days,
              'quote_way':this.tagData.data.quote_way,
              'service_mode':this.tagData.data.service_mode,
              'customer_id':this.tagData.data.customer_id,
              'quote_price':this.tagData.data.quote_price,
              'contract_id':this.tagData.data.contract_id,
              'plugin_id':this.tagData.data.plugin_id,
              'salesman_name':this.tagData.data.salesman_name,
              'salesman_mobile':this.tagData.data.salesman_mobile
            }, res => {
              if(res.url){
                setTimeout(() => {
                  src = res.url;
                  iframe.setAttribute("src", src);                  
                }, 100);
              }              
            });
          }else{
            if (this.tagData.data.appType) {
              this.isShowApply = true;
              const appType = parseInt(this.tagData.data.appType);
              const code = this.tagData.data.code;
              this.clientType = appType;
              this.inviteCode = code;
              const url = this.tagData.data.content + '&appType=' + appType;
              src = null;
  
            } else {
              src = this.tagData.data.content;
              // this.iab.create(src,'_self');
              iframe.setAttribute("src", src);
            }
          }
          

        }

      } catch (error) {
        iframe.setAttribute("src", "");
      }
    }, 100);
  }
}
